package com.xrui.hbase.schema.impl;

import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

import com.xrui.hbase.schema.SchemaType;

import java.util.List;


public final class CustomCellSchema extends GenericCellSchema {
    private static final String delimiter = "|";
    private String schemaId;
    private String cellEncoder;
    private String cellDecoder;

    private CustomCellSchema(String id, String value, String compression, String converter) {
        super(SchemaType.CUSTOM, value, compression, converter);
        List<String> codec = Lists.newArrayList(Splitter.on(delimiter).limit(2).split(value));
        schemaId = id;
        cellEncoder = codec.get(0);
        cellDecoder = codec.get(1);
    }

    public static CustomCellSchema create(String name, String value, String compression, String converter) {
        return new CustomCellSchema(name, value, compression, converter);
    }

    public static CustomCellSchema create(String name, String value) {
        return new CustomCellSchema(name, value, null, null);
    }

    @Override
    public String getValue() {
        return cellEncoder + delimiter + cellDecoder;
    }

    public String getCellEncoder() {
        return cellEncoder;
    }

    public String getCellDecoder() {
        return cellDecoder;
    }

    public String getId() {
        return schemaId;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(CustomCellSchema.class)
            .add("type", mSchemaType)
            .add("encoder", cellEncoder)
            .add("decoder", cellDecoder)
            .add("converter", mConverter)
            .add("compression", mCompressionProvider)
            .toString();
    }
}
